import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import { isLogined } from "../utils/auth";

Vue.use(VueRouter);
const routes = [
  {
    name: "Home",
    path: "/",
    component: HomeView,
  },
  {
    name: "List",
    path: "/about",

    component: () => import("../views/AboutView.vue"),
  },
  {
    name: "User",
    path: "/user",
    meta: {
      islogin: true,
    },
    component: () => import("../views/User.vue"),
  },
  {
    name: "UserInfo",
    path: "userinfo",
    meta: {
      islogin: true,
    },
    component:()=>import("../views/Usermore/Userinfo.vue")
  },
  {
    name: "UserInfoalt",
    path: "userinfoalt",
    meta: {
      islogin: true,
    },
    component:()=>import("../views/Usermore/Userinfoalt.vue")
  },
  {
    name: "Cart",
    path: "/cart",
    meta: {
      ishidden: true,
      islogin: true,
    },
    component: () => import("../views/Cart.vue"),
  },
  {
    name: "Detail",
    path: "/about/:id",
    meta: { ishidden: true },
    component: () => import("../views/Detail.vue"),
  },
  {
    name: "Login",
    path: "/login",
    meta: { ishidden: true },
    component: () => import("../views/Login.vue"),
  },
  {
    name: "Reg",
    path: "/reg",
    meta: { ishidden: true },
    component: () => import("../views/Reg.vue"),
  },
  {
    name: "Address",
    path: "/address",
    meta: { ishidden: true },
    component: () => import("../views/address.vue"),
  },
  {
    name: "conAddress",
    path: "/conaddress",
    meta: { ishidden: true },
    component: () => import("../views/ConfimAddress.vue"),
  },
  {
    name: "AddressEdit",
    path: "/addedit",
    meta: { ishidden: true },
    component: () => import("../views/addressedit.vue"),
  },
  {
    name: "OrderSuccess",
    path: "/ordersuccess",
    meta: { ishidden: true },
    component: () => import("../views/ordersuccess.vue"),
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.meta.islogin) {
    if (isLogined()) {
      next();
    } else {
      next({
        name: "Login",
      });
    }
  } else {
    next();
  }
});

export default router;
